package fichier;

import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;

@SuppressWarnings("serial")
public class ChangeMdP extends JDialog implements ActionListener{
	
	private User user;
	private String oldPassword;
	private String password;
	private String passwordVerif;
	private JPanel panelInfos;
	private JPanel panelButton;
	private JButton butValider;
	private JButton butAnnuler;
	private JLabel oldPasswordLab;
	private JLabel passwordLab;
	private JLabel passwordVerifLab;
	private JPasswordField oldPasswordTxt;
	private JPasswordField passwordTxt;
	private JPasswordField passwordVerifTxt;
	private String newMdp;
	@SuppressWarnings("unused")
	private int userId;
	
	public String getPassword(){
		return this.password;
	}
	
	public String getPasswordVerif(){
		return this.passwordVerif;
	}
	
	public String getoldPassword(){
		return this.oldPassword;
	}
	
	public User getUser(){
		return this.user;
	}
	
	
	public ChangeMdP(JFrame plop, Boolean plop2){
		super(plop, plop2);
		this.initPanels();
		this.setTitre();
		this.setBounds(200,200,400,250);
		this.setVisible(true);
		
	}
		
		
		
	public void initPanels( ){					
	//r�cup�ration du contenu de la fen�tre
		Container contenu= this.getContentPane();
		contenu.setLayout(null);
						
		this.initPanelInfos();
		//this.initPanelButtons();
					
		// ajout des composants au contenu de la fen�tre
		contenu.add(this.panelInfos);
		//contenu.add(this.panelButton) ;
					
		this.setBoundsPanels();
	}
	
	public void setBoundsPanels(){
		this.panelInfos.setBounds(0, 20, 400, 200);
		//this.panelButton.setBounds(90, 150, 200, 40);
		
	}
		
		
	
	public void initPanelInfos(){	
		this.panelInfos = new JPanel();
		this.oldPassword = new String("");
		this.password = new String("");
		this.passwordVerif = new String("");
		this.oldPasswordLab = new JLabel("Ancien mot de passe");
		this.passwordLab = new JLabel("Mot de passe");
		this.passwordVerifLab = new JLabel("Confirmer mot de passe");
		this.oldPasswordTxt = new JPasswordField("");
		this.passwordTxt = new JPasswordField("");
		this.passwordVerifTxt = new JPasswordField("");
		
		this.butValider = new JButton("Valider");
		this.butAnnuler = new JButton("Annuler");
	
		this.oldPasswordLab.setBounds(40,20,170,20);
		this.passwordLab.setBounds(40,50,170,20);
		this.passwordVerifLab.setBounds(40,80,170,20);
		this.oldPasswordTxt.setBounds(240,20,120,20);
		this.passwordTxt.setBounds(240,50,120,20);
		this.passwordVerifTxt.setBounds(240,80,120,20);
		this.butValider.setBounds(100,130,80,20);
		this.butAnnuler.setBounds(240,130,80,20);
		
		this.getContentPane().setLayout(null);
		this.panelInfos.setLayout(null);
		this.panelInfos.add(this.oldPasswordLab);
		this.panelInfos.add(this.oldPasswordTxt);
		this.panelInfos.add(this.passwordLab);
		this.panelInfos.add(this.passwordTxt);
		this.panelInfos.add(this.passwordVerifLab);
		this.panelInfos.add(this.passwordVerifTxt);
		this.panelInfos.add(this.butValider);
		this.panelInfos.add(this.butAnnuler);
			
		this.butValider.addActionListener(this);
		this.butAnnuler.addActionListener(this);
		
	}
	
	public void initPanelButtons(){
		this.panelButton = new JPanel();
		this.butValider = new JButton("Valider");
		this.butAnnuler = new JButton("Annuler");
		
		this.panelButton.add(this.butValider);
		this.panelButton.add(this.butAnnuler);
		this.butValider.setBounds(0,0,80,20);
		this.butAnnuler.setBounds(0,0,80,20);
		this.butValider.addActionListener(this);
		this.butAnnuler.addActionListener(this);
		
	}
		
	
	
	public void setTitre() {
		this.setTitle("Changement de mot de passe");
	}

	public  void annule(){
		this.dispose();
	}
	
	public String getOldPassword(){
		SelectData.REQUETE = "SELECT mdp_user FROM users WHERE id_user="+ Connexion.autorisation;
		ResultSet res = SelectData.startSelect();
		String oldMdp = new String();
		try{
			while(res.next()){
				oldMdp = res.getString("mdp_user");
				
			}
			
		}
		catch(SQLException e){
			System.out.println("error");
		}
		//return "";	
		return oldMdp;
		}
	
	
	
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.butValider){
			if(passwordTxt.getText().equals("") && this.passwordVerifTxt.getText().equals(""))
				JOptionPane.showMessageDialog(this, "Tous les champs sont vides !", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			
			else if(passwordTxt.getText().equals(""))
				JOptionPane.showMessageDialog(this, "Vous n'avez pas rentré un nouveau mot de passe", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			
			else if(!this.passwordTxt.getText().equals(this.passwordVerifTxt.getText()))
				JOptionPane.showMessageDialog(this, "La confirmation du nouveau mot de passe ne correspond pas avec le nouveau mot de passe rentré ! ", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			
			else if(!this.oldPasswordTxt.getText().equals(getOldPassword()))
				JOptionPane.showMessageDialog(this, "L'ancien mot de passe ne correspond pas !", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
			
			else{
					//String newMdp = new String();
					newMdp = this.passwordTxt.getText();
					SelectData.REQUETE = "UPDATE users SET mdp_user =\'"+ newMdp+"\' WHERE id_user="+Connexion.autorisation;
					SelectData.startUpdate();
					if(main.testUpdate){
						JOptionPane.showMessageDialog(this, "Le mot de passe a bien été changé", "Attention",1,new ImageIcon(getClass().getResource("/icon/ok.png")));
						this.dispose();
					}
					else
						JOptionPane.showMessageDialog(this, "Le mot de passe n'a pas pu être changé", "Attention",1,new ImageIcon(getClass().getResource("/icon/warn.png")));
				}
		}
		
		if (e.getSource() == this.butAnnuler){
			int rep = JOptionPane.showConfirmDialog(this, "Etes-vous sûr de vouloir quitter ?", "Confirmation annulation",JOptionPane.YES_NO_OPTION);
			if (rep == JOptionPane.OK_OPTION)
				this.annule();
		}

	}
	
	

	
	
}